package com.chinatcc.proxy.util;

import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 * 邮件发�?工具
 * 
 * @author giles
 * 
 */
public class MailUtil {

	/**
	 * 系统发�?邮件
	 * 
	 * @param 收件�?
	 *            not null
	 * @param 标题
	 *            not null
	 * @param 内容
	 *            not null
	 * @param 文件�?
	 *            null
	 * @param 文件路径
	 *            null
	 * @throws MessagingException
	 *             邮件发�?异常
	 */
	public static void sendSysMail(String to, String title, String content,
			String filename, String filepath) throws MessagingException {
		JavaMailSenderImpl senderImpl = (JavaMailSenderImpl) new ClassPathXmlApplicationContext(
				"applicationContext.xml").getBean("mailSender");
		MimeMessage mailMessage = senderImpl.createMimeMessage();
		MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,
				true);
		messageHelper.setTo(to);
		messageHelper.setFrom(senderImpl.getUsername());
		messageHelper.setSubject(title);
		messageHelper.setText(content, true);
		FileSystemResource file = null;
		if (null != filepath) {
			file = new FileSystemResource(new File(filepath));
		}
		if (null != filename) {
			messageHelper.addAttachment(filename, file);
		}
		
		senderImpl.send(mailMessage);

		System.out.println("==成功发�?邮件==");
	}

	/**
	 * 用户可发送邮�?
	 * 
	 * @param username
	 *            发件人用户名
	 * @param pwd
	 *            发件人密�?
	 * @param host
	 *            发件人smtp主机
	 * @param port
	 *            发件人smtp端口�?
	 * @param to
	 *            收件人邮�?
	 * @param title
	 *            邮件标题
	 * @param content
	 *            邮件内容
	 * @param filename
	 *            文件�?
	 * @param filepath
	 *            文件路径
	 * @throws MessagingException
	 */
	public static void sendUserMail(String username, String pwd, String host,
			int port, String to, String title, String content, String filename,
			String filepath) throws MessagingException {
		JavaMailSenderImpl senderImpl = (JavaMailSenderImpl) new ClassPathXmlApplicationContext(
				"applicationContext.xml").getBean("mailSender");
		senderImpl.setUsername(username); // 用户�?
		senderImpl.setPassword(pwd); // 密码
		senderImpl.setHost(host); // smtp主机�?
		senderImpl.setPort(port); // smtp端口�?
		MimeMessage mailMessage = senderImpl.createMimeMessage();
		MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,
				true);

		messageHelper.setTo(to);
		messageHelper.setFrom(senderImpl.getUsername());
		messageHelper.setSubject(title);
		messageHelper.setText(content, true);
		FileSystemResource file = null;
		if (null != filepath) {
			file = new FileSystemResource(new File(filepath));
		}
		if (null != filename) {
			messageHelper.addAttachment(filename, file);
		}

		senderImpl.send(mailMessage);

		System.out.println("==成功发�?邮件==");
	}
}
